/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package data.contact;

import data.email.EmailAddress;
import enums.ContactPart;
import java.io.*;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Observable;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import sun.reflect.generics.reflectiveObjects.NotImplementedException;

/**
 *
 * @author Arcanis
 */
public class ContactFolder extends Observable implements Serializable {

    private String name;
    private ContactMap contacts = new ContactMap();

    public ContactFolder(String name) {
        this.name = name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public List<Contact> getContacts() {
        List<Contact> temp = new LinkedList<>();
        for (Map.Entry<EmailAddress, Contact> entry : contacts.entrySet()) {
            Contact contact1 = entry.getValue();
            temp.add(contact1);
        }
        return temp;
    }

    public boolean addContact(Contact contact) {
        if (!contacts.containsKey(contact.getAddress())) {
            contacts.put(contact.getAddress(), contact);
            setChanged();
            return true;
        }
        return false;
    }

    public boolean deleteContact(EmailAddress email) {
        if (contacts.remove(email) != null) {
            setChanged();
            return true;
        }
        return false;
    }

    public Contact getContact(EmailAddress email) {
        if (!contacts.containsKey(email)) {
            addContact(new Contact(new EmailAddress(email.getAddress())));
            notifyObservers();
        }
        return contacts.get(email);
    }

    public List<Contact> getContacts(ContactPart[] searchType, String searched) {
        //@TODO Necka - returns contacts which contains searched strings in fields searchType. Look to enum, which columns are searchable

        //you can use
        //EventBusService.publish(new OpenWaitingWindowEvent("Hledání"));
        //EventBusService.publish(new OpenWaitingWindowEvent("Hledání",100));
        //EventBusService.publish(new UpdateProgressBar(20));
        //EventBusService.publish(new CloseWaitingWindowEvent());
        throw new NotImplementedException();
    }

    public void load(File file) {
        FileInputStream st = null;
        GZIPInputStream gz = null;
        try {
            st = new FileInputStream(file);
            gz = new GZIPInputStream(st);
            try (ObjectInputStream st3 = new ObjectInputStream(gz)) {
                Object o = st3.readObject();
                if (o instanceof ContactFolder) {
                    ContactFolder cf = (ContactFolder) o;
//                    System.out.println(cf.name);
//                    System.out.println(cf.contacts.size());
                    this.name = cf.name;
                    this.contacts.clear();
                    this.contacts.putAll(cf.contacts);
                }
            }
        } catch (ClassNotFoundException | IOException ex) {
            Logger.getLogger(ContactFolder.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                st.close();
            } catch (IOException ex) {
                Logger.getLogger(ContactFolder.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void save(File file) {
        FileOutputStream st = null;
        GZIPOutputStream gz = null;
        try {
            st = new FileOutputStream(file);
            gz = new GZIPOutputStream(st);
            try (ObjectOutputStream st3 = new ObjectOutputStream(gz)) {
                st3.writeObject(this);
            }
        } catch (FileNotFoundException ex) {
            Logger.getLogger(ContactFolder.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(ContactFolder.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                st.close();
            } catch (IOException ex) {
                Logger.getLogger(ContactFolder.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
    }

    public void sendInit() {
        setChanged();
        notifyObservers();
    }
}
